Imports System.Data

Partial Class Accounting_AddPayment
    Inherits WebPageCommon

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim cf As New CIMSfunc

        cf.setNumericFormat(Amount_txt)
        cf = Nothing

        If Not Page.IsPostBack Then
            Call PopulateDropDown()

            If Not IsNothing(Session("pagesource")) Then
                ViewState.Add("pagesource", Session("pagesource"))
                Session.Remove("pagesource")
            End If

            Call InitVars()

            If Not IsNothing(ViewState("Seq_Cd")) Then
                PolicySearch.Visible = False
                Policysearch_btn.Visible = True
            Else
                PolicySearch.Visible = True
                Policysearch_btn.Visible = False
            End If

            Call LoadPayment()
            Call LoadARType()

            Call PolicySearch.Search()
        End If

        Call SetPageSecurity(divMain)
    End Sub

    Protected Sub PolicySearch_Policy_Selected() Handles PolicySearch.Policy_Selected
        ViewState.Add("Seq_Cd", PolicySearch.Policy_cd)
        PolicySearch.Visible = False
        Policysearch_btn.Visible = True

        With PolicySearch
            ViewState.Add("Seq_CD", .Policy_cd)

            ClientName_txt.Text = .ClientName
            PolicyNo_txt.Text = .PolicyNo
            Product_txt.Text = .Product
            Provider_txt.Text = .Provider
            Amount_txt.Text = .ContributionAmount
            Currency_cmb.SelectedValue = .Currency_Cd
        End With

    End Sub

    Protected Sub Policysearch_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Policysearch_btn.Click
        PolicySearch.Visible = True
        Policysearch_btn.Visible = False
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim pfunc As New PolyFunc.ASPFunc

        If Not IsNothing(ViewState("Seq_CD")) Then

            Call SavePremium()
            Call SavePayment()

            pfunc.MessageAlert(Me, "Record Saved")
            btnPrint.Visible = True
        Else
            pfunc.MessageAlert(Me, "Please select a policy before saving")
        End If

        pfunc = Nothing
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        Dim strPath As String = ViewState("pagesource")
        'Call InitSessions()
        Response.Redirect(strPath)
    End Sub

    Private Sub PopulateDropDown()
        Dim lkup As New LookUp.FillLookUp

        With lkup
            .FillDropDown(Me.Currency_cmb, LookUp.FillLookUp.eLookUpType.other, "CURRENCY")
            .FillDropDown(Me.PaymentType_cmb, LookUp.FillLookUp.eLookUpType.other, "PAYMENT_FORMS")
            .FillDropDown(Me.PaymentStatus_cmb, LookUp.FillLookUp.eLookUpType.other, "PAYMENT_STATUS")
        End With

        lkup = Nothing
    End Sub

    Private Sub LoadPayment()
        If Not IsNothing(ViewState("Seq_CD")) And Not IsNothing(ViewState("CID")) Then
            Dim vo As New DAOMain.VW_POLICY_PREMIUM_DTL_REC_VO
            Dim dao As New DAOMain.VW_POLICY_PREMIUM_DTL_REC_DAO

            PolicySearch.Visible = False
            Policysearch_btn.Visible = False

            vo.POLICY_SEQ_CD = ViewState("Seq_CD")
            vo.POLICY_PREMIUM_CD = ViewState("CID")

            Dim dt As DataTable = dao.Search(vo)

            If dt.Rows.Count > 0 Then
                With dt.Rows(0)
                    Dim cf As New CIMSfunc

                    ClientName_txt.Text = IIf((.Item("Client_Name") Is DBNull.Value), "", .Item("Client_Name")) '.Item("Client_Name")
                    PolicyNo_txt.Text = .Item("policy_no")
                    Product_txt.Text = .Item("prdt_desc")
                    Provider_txt.Text = .Item("provider_desc")
                    Amount_txt.Text = IIf((.Item("contribution_amt") Is DBNull.Value), "", cf.loadFormat(.Item("contribution_amt")))
                    Currency_cmb.SelectedValue = .Item("currency_cd")
                    txtRemarks.Text = IIf((.Item("Remarks") Is DBNull.Value), "", .Item("Remarks"))
                    PaymentType_cmb.SelectedValue = IIf((.Item("payment_form") Is DBNull.Value), "", .Item("payment_form"))
                    PaymentStatus_cmb.SelectedValue = IIf((.Item("payment_status") Is DBNull.Value), "", .Item("payment_status"))

                    PaymentNo_txt.Text = IIf((.Item("payment_no") Is DBNull.Value), "", .Item("payment_no"))
                    If .Item("payment_dt") Is DBNull.Value Then
                        PaymentDT_txt.Text = ""
                        btnPrint.Visible = False
                    Else
                        PaymentDT_txt.Text = cf.ParseGetDate(.Item("payment_dt"))
                        'PaymentNo_txt.Enabled = False
                    End If

                    Dim i As Integer = GetAutoNo()

                    If i <> 0 Then
                        PaymentNo_txt.Enabled = False
                    Else
                        PaymentNo_txt.Enabled = True
                    End If

                    If .Item("ar_date") Is DBNull.Value Then
                        ARDT_txt.Text = ""
                    Else
                        ARDT_txt.Text = .Item("ar_date")
                        ARDT_txt.Text = cf.ParseGetDate(ARDT_txt.Text)
                    End If

                    DueDT_txt.Text = cf.ParseGetDate(.Item("policy_due_dt"))

                    cf = Nothing
                End With
        End If

        vo = Nothing
        dao = Nothing
        End If
    End Sub

    Private Sub LoadARType()
        If Not IsNothing(ViewState("CName")) Then
            Dim dao As New DAOMain.AR_TYPE_PAYMENT_DTL_DAO
            Dim vo As New DAOMain.AR_TYPE_PAYMENT_DTL_VO

            vo.PAYMENT_SEQ_CD = ViewState("CName")
            Dim dt As DataTable = dao.Search(vo)

            If dt.Rows.Count > 0 Then
                rblPrint.SelectedValue = dt.Rows(0)("AR_TYPE_SEQ_CD")
                rblPrint.Enabled = False
            End If

            dt = Nothing
            dao = Nothing
            vo = Nothing
        End If
    End Sub

    Private Function SavePremium() As Boolean
        Dim dao As New DAOMain.POLICY_PREMIUM_DTL_DAO
        Dim vo As New DAOMain.POLICY_PREMIUM_DTL_VO
        Dim cf As New CIMSfunc

        If Not IsNothing(ViewState("Seq_CD")) Then
            With vo
                .POLICY_SEQ_CD = ViewState("Seq_CD")

                .PAY_TAG = 1
                .CONTRIBUTION_AMT = Val(cf.saveFormat(Amount_txt.Text))
                .CURRENCY_CD = Currency_cmb.SelectedValue.ToString
                .POLICY_DUE_DT = cf.ParseSetDate(DueDT_txt.Text)

                Select Case IsNothing(ViewState("CID"))
                    Case True
                        'check 
                        .POLICY_PREMIUM_CD = cf.GetLastID("policy_premium_cd", "policy_premium_dtl")
                        ViewState.Add("CID", .POLICY_PREMIUM_CD)

                        dao.Create_Record(vo)
                    Case False
                        .POLICY_PREMIUM_CD = ViewState("CID")
                        dao.Update(vo)
                End Select
            End With
        End If

        dao = Nothing
        vo = Nothing
        cf = Nothing
    End Function

    Private Function SavePayment() As Boolean
        Dim dao As New DAOMain.PAYMENT_DTL_DAO
        Dim vo As New DAOMain.PAYMENT_DTL_VO
        Dim cf As New CIMSfunc
        Dim blnAutoNum As Boolean = False

        With vo
            .POLICY_SEQ_CD = ViewState("Seq_CD")
            .POLICY_PREMIUM_CD = ViewState("CID")
            .PAYMENT_AMT = Val(cf.saveFormat(Amount_txt.Text))

            .PAYMENT_NO = PaymentNo_txt.Text

            .AR_DATE = cf.ParseSetDate(ARDT_txt.Text)
            .PAYMENT_DT = cf.ParseSetDate(PaymentDT_txt.Text)
            .PAYMENT_STATUS = PaymentStatus_cmb.SelectedValue.ToString
            .PAYMENT_FORM = PaymentType_cmb.SelectedValue.ToString
            .REMARKS = txtRemarks.Text
            .CONFIRM_NO = ""
            .DISPATCH_NO = ""

            Select Case IsNothing(ViewState("CName"))
                Case True
                    .PAYMENT_SEQ_CD = cf.GetLastID("payment_seq_cd", "payment_dtl")
                    .PAYMENT_CREA_BY = ViewState("UserName")
                    .PAYMENT_CREA_DT = Now

                    dao.Create_Record(vo)
                    SaveARType(.PAYMENT_SEQ_CD, "Insert")
                    If blnAutoNum = True Then UpdateAutoNo(Val(.PAYMENT_NO))
                Case False
                    .PAYMENT_SEQ_CD = ViewState("CName")
                    .PAYMENT_LAST_UPD_BY = ViewState("UserName")
                    .PAYMENT_LAST_UPD_DT = Now

                    dao.Update(vo)
                    SaveARType(.PAYMENT_SEQ_CD, "Update")
            End Select

        End With

        dao = Nothing
        vo = Nothing
        cf = Nothing
    End Function

    Private Sub SaveARType(ByVal mCd As String, ByVal mAction As String)
        Dim dao As New DAOMain.AR_TYPE_PAYMENT_DTL_DAO
        Dim vo As New DAOMain.AR_TYPE_PAYMENT_DTL_VO

        With vo
            .PAYMENT_SEQ_CD = mCd
            .AR_TYPE_SEQ_CD = rblPrint.SelectedValue
        End With

        Select Case mAction
            Case "Insert"
                dao.Create_Record(vo)
            Case "Update"
                dao.Update(vo)
        End Select

        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If Not IsNothing(ViewState("CID")) Then
            Dim dao As New DAOMain.VW_POLICY_PREMIUM_DTL_REC_DAO
            Dim vo As New DAOMain.VW_POLICY_PREMIUM_DTL_REC_VO

            vo.POLICY_PREMIUM_CD = ViewState("CID")
            Dim dt As DataTable = dao.Search(vo)

            If dt.Rows.Count > 0 Then
                Dim cf As New CIMSfunc
                dt.Rows(0)("remarks") = cf.cNumToWord(CStr(dt.Rows(0)("payment_amt")))
                cf = Nothing

                Dim cr As New CReports

                cr.ShowRpt(Me.rblPrint.SelectedValue, CReports.RptType.PDF_Report, Me, dt, "vw_policy_premium_dtl_rec")
                cr = Nothing
            End If

            dao = Nothing
            vo = Nothing
        End If
    End Sub

    Private Function GetAutoNo() As Integer
        Dim dao As New DAOMain.AR_TYPE_MSTR_DAO
        Dim vo As New DAOMain.AR_TYPE_MSTR_VO
        Dim iReturn As Integer = 0

        vo.AR_TYPE_SEQ_CD = rblPrint.SelectedValue

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            iReturn = dt.Rows(0)("AR_TYPE_CURR_NO")
        End If

        dt = Nothing
        dao = Nothing
        vo = Nothing

        Return iReturn
    End Function

    Private Sub UpdateAutoNo(ByVal curr_no As Long)
        Dim dt As DataTable = getArTypeDetail(rblPrint.SelectedValue)

        If dt.Rows.Count > 0 Then
            Dim dao As New DAOMain.AR_TYPE_MSTR_DAO
            Dim vo As New DAOMain.AR_TYPE_MSTR_VO
            Dim iReturn As Integer = 0


            With vo
                .AR_TYPE_SEQ_CD = rblPrint.SelectedValue
                .AR_TYPE_CURR_NO = curr_no + 1
                .AR_TYPE_DESC = dt.Rows(0)("AR_TYPE_DESC")
                .AR_TYPE_START_NO = dt.Rows(0)("AR_TYPE_START_NO")
                .AR_TYPE_LAST_UPD_BY = Session("UserName")
                .AR_TYPE_LAST_UPD_DT = Now
            End With

            dao.Update(vo)

            PaymentNo_txt.Text = vo.AR_TYPE_CURR_NO
            dao = Nothing
            vo = Nothing
        End If

        dt = Nothing
    End Sub

    Private Function getArTypeDetail(ByVal AR_TYPE_SEQ_CD As String) As DataTable
        Dim dao As New DAOMain.AR_TYPE_MSTR_DAO
        Dim vo As New DAOMain.AR_TYPE_MSTR_VO

        vo.AR_TYPE_SEQ_CD = AR_TYPE_SEQ_CD
        Dim dt As DataTable = dao.Search(vo)

        dao = Nothing
        vo = Nothing

        Return dt
    End Function

    Protected Sub rblPrint_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rblPrint.SelectedIndexChanged
        Dim i As Integer = GetAutoNo()

        If i <> 0 Then
            PaymentNo_txt.Enabled = False
        Else
            PaymentNo_txt.Text = ""
            PaymentNo_txt.Enabled = True
        End If
    End Sub

End Class
